package org.BiteTheDice;
import org.BiteTheDice.R;
import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.DialogInterface;

public class BiteTheDice extends Activity implements OnClickListener{

	public static final String PREF_FILE_NAME = "PrefFile";

	protected static int SCORE0 = 0 ;
	protected static  int SCORE1 = 0 ;
	protected static  int SCORE2 = 0 ;
	protected static  int SCORE3 = 0 ;
	protected static  int SCORE4 = 0 ;
	protected static  int SCORE5 = 0 ;
	protected static  int SCORE6 = 0 ;
	protected static  int SCORE7 = 0 ;
	protected static  int SCORE8 = 0 ;
	protected static  int SCORE9 = 0 ;
	public static int currentScore = 0 ;
	private static boolean highScoresClick=false;
	private static boolean helpClick=false;
	private static boolean aboutClick=false;

	protected static String currentWinner = "Player 1" ;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Set up click listeners for all the buttons
		View newButton = findViewById(R.id.new_button);
		newButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.yellow_button));
		newButton.setOnClickListener(this);
		View continueButton = findViewById(R.id.highscores_button);
		continueButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.green_button));
		continueButton.setOnClickListener(this);
		View aboutButton = findViewById(R.id.about_button);
		aboutButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.green_button));
		aboutButton.setOnClickListener(this);
		View exitButton = findViewById(R.id.exit_button);
		exitButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.green_button));
		exitButton.setOnClickListener(this);
		View helpButton = findViewById(R.id.help_button);
    	helpButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.green_button));
		helpButton.setOnClickListener(this);
		View settingsButton = findViewById(R.id.settings_button);
		settingsButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.yellow_button));
		settingsButton.setOnClickListener(this);
		SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME,MODE_PRIVATE);
		//On Create, Load HighScores
		SCORE0 = preferences.getInt("SCORE0", 0);
		SCORE1 = preferences.getInt("SCORE1", 0);
		SCORE2 = preferences.getInt("SCORE2", 0);
		SCORE3 = preferences.getInt("SCORE3", 0);
		SCORE4 = preferences.getInt("SCORE4", 0);
		SCORE5 = preferences.getInt("SCORE5", 0);
		SCORE6 = preferences.getInt("SCORE6", 0);
		SCORE7 = preferences.getInt("SCORE7", 0);
		SCORE8 = preferences.getInt("SCORE8", 0);
		SCORE9 = preferences.getInt("SCORE9", 0);

	}

	/**
	 * Connect Buttons With XML's
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.about_button:
			Intent i = new Intent(this, About.class);
			startActivity(i);
			aboutClick=true;
			break;
		case R.id.new_button:
			openNewGameDialog();
			break;
		case R.id.exit_button:
			moveTaskToBack(true);
			break;
		case R.id.highscores_button:
			Intent j = new Intent(this, HighScores.class);
			startActivity(j);
			highScoresClick=true;
			break;
		case R.id.help_button:
			Intent h = new Intent(this, Help.class);
			startActivity(h);
			highScoresClick=true;
			break;
			case R.id.settings_button:
			 startActivity(new Intent(this, Prefs.class));
			 break;
		}

	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			moveTaskToBack(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * Opens Select Game type Menu 
	 */
	private void openNewGameDialog() {
		ColorAlertDialog.Builder dialog = new ColorAlertDialog.Builder(this, R.style.YellowAlertDialogTheme);
		dialog.setTitle("Game Type");
		dialog.setPositiveButton("Single Player", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialoginterface,
					int i) {
				startGame(0);	
			}
		});
		dialog.setNegativeButton("Two Players", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialoginterface,
					int i) {
				startGame(1);	
			}
		});	
		dialog.create();
		dialog.show();
	}
/*setItems(R.array.type,
				new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialoginterface,
					int i) {
				startGame(i);	
			}
		})*/
	/**
	 * Start Game
	 */

	private void startGame(int i) {
		if (i==0){
			//	run single Player game...
			RunGame.setNumPlayer(1);
			Intent j = new Intent(this, RunGame.class);
			startActivity(j);
		}
		else{
			//run multi player game...
			RunGame.setNumPlayer(2);
			Intent j = new Intent(this, RunGame.class);
			startActivity(j);
		}
			
	}


	/**
	 * Run Settings Menu
	 * getMenuInflater( ) returns an instance of MenuInflater that we use to read
	 * the menu definition from XML and turns it into a real view.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}
	
	/**
	 * When the user selects any menu item,
	 *  onOptionsItemSelected( ) will be called
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			startActivity(new Intent(this, Prefs.class));
			return true;
		}
		return false;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (startMusicOver)
			Music.play(this, R.raw.menumusic);
	}
	private static boolean startMusicOver=true;
	@Override
	protected void onPause() {
		super.onPause();
		if (highScoresClick||aboutClick || helpClick){
			highScoresClick=false;
			aboutClick=false;
			helpClick=false;
			startMusicOver=false;
		}
		else{
			Music.stop(this);
			startMusicOver=true;
		}
	}



	protected void saveScores(int score){
		if (score<=SCORE9)
			return;
		ArrayList<Integer> scores=new ArrayList<Integer>();
		scores.add(score);
		scores.add(SCORE0);
		scores.add(SCORE1);
		scores.add(SCORE2);
		scores.add(SCORE3);
		scores.add(SCORE4);
		scores.add(SCORE5);
		scores.add(SCORE6);
		scores.add(SCORE7);
		scores.add(SCORE8);
		scores.add(SCORE9);


		Collections.sort(scores, new Intcomp());

		SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME,MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		int newScore=scores.get(0);
		SCORE0=newScore;
		editor.putInt("SCORE0", newScore); // value to store

		newScore=scores.get(1);
		SCORE1=newScore;
		editor.putInt("SCORE1", newScore); // value to store

		newScore=scores.get(2);
		SCORE2=newScore;
		editor.putInt("SCORE2", newScore); // value to store

		newScore=scores.get(3);
		SCORE3=newScore;
		editor.putInt("SCORE3", newScore); // value to store

		newScore=scores.get(4);
		SCORE4=newScore;
		editor.putInt("SCORE4", newScore); // value to store

		newScore=scores.get(5);
		SCORE5=newScore;
		editor.putInt("SCORE5", newScore); // value to store

		newScore=scores.get(6);
		SCORE6=newScore;
		editor.putInt("SCORE6", newScore); // value to store

		newScore=scores.get(7);
		SCORE7=newScore;
		editor.putInt("SCORE7", newScore); // value to store

		newScore=scores.get(8);
		SCORE8=newScore;
		editor.putInt("SCORE8", newScore); // value to store

		newScore=scores.get(9);
		SCORE9=newScore;
		editor.putInt("SCORE9", newScore); // value to store

		editor.commit();

	}

}